package com.chen.root.enums;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @author: ~龙~
 * @date: 2023/6/27 17:41
 **/
@Getter
@AllArgsConstructor
public enum DriveEnum {

    /**
     * mysql
     */
    MYSQL(
            "mysql",
            "com.mysql.cj.jdbc.Driver",
            "SELECT TABLE_NAME as tableName,TABLE_COMMENT as tableComment FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='{}'"
    ),

    /**
     * oracle
     */
    ORACLE(
            "oracle",
            "oracle.jdbc.OracleDriver",
            "SELECT TABLE_NAME as \"tableName\",COMMENTS as \"tableComment\" FROM ALL_TAB_COMMENTS WHERE OWNER='{}'"
    ),

    /**
     * db2
     */
    DB2(
            "db2",
            "com.ibm.db2.jcc.DB2Driver",
            "SELECT TABNAME as tableName, REMARKS as tableComment FROM SYSCAT.TABLES WHERE TABSCHEMA = '{}'"
    ),

    /**
     * 人大金仓
     */
    KING_BASE8(
            "kingbase8",
            "com.kingbase8.Driver",
            "SELECT t1.relname as tableName, t2.description as tableComment\n" +
                    "FROM sys_class t1\n" +
                    "LEFT JOIN (SELECT objoid, description FROM pg_description WHERE objsubid = 0) t2 ON t1.oid = t2.objoid\n" +
                    "WHERE t1.relkind = 'r' AND t1.relnamespace = (SELECT oid FROM sys_namespace WHERE nspname='{}')"
    );

    public String type;
    public String drive;
    public String tableSql;

    public static String getDrive(String type){
        DriveEnum driveEnum = typeMate(type);
        return driveEnum != null ? driveEnum.drive : null;
    }

    public static String getTableSql(String type){
        DriveEnum driveEnum = typeMate(type);
        return driveEnum != null ? driveEnum.tableSql : null;
    }

    public static DriveEnum typeMate(String type){
        for (DriveEnum driveEnum : DriveEnum.values()) {
            if (StrUtil.isNotBlank(type) && driveEnum.type.equals(type.toLowerCase())){
                return driveEnum;
            }
        }
        return null;
    }
}
